<?php

/**
 * 默认控制器
 */
class Controller_Rulesetting extends Controller_Abstract
{

	/**1**/
	/*Host Manage*/
	function actionRuleAddform()
    {
		$this->_view['form'] = new Form_RuleAdd(url('default::rulesetting/add'));
    }

	/*Host Manage*/
	function actionRuleGLAddform()
    {
		$this->_view['form'] = new Form_RuleAdd(url('default::rulesetting/add'));
		$this->_view['form']->import(array('ruleflag'=>'1'));
    }

	/*Host Manage*/
	function actionRuleBJAddform()
    {
		$this->_view['form'] = new Form_RuleAdd(url('default::rulesetting/add'));
		$this->_view['form']->import(array('ruleflag'=>'0'));
    }


	/**1**/
	function actionAdd()
    {
        $show_box['title'] = '提交规则';
		$form = new Form_RuleAdd(url('default::rulesetting/add'));
		if ($this->_app->currentUserRoles())
        {
		Operate::everyOperate($_SESSION['acl_task_userdata']['account'],'','1',''.$show_box['title'],'');
			
		try{
			// 查询指定用户 ID`keyword`,`category`,`ruleflag`,`groupid`
			//`rulename`,`ruleflag`
			$edit_one = Config::find('keyword = ? and category = ? and ruleflag =? and groupid =?', $_POST['keyword'],$_POST['category'],$_POST['ruleflag'],$_POST['groupid'])->query();
			
			//dump($edit_one->id());
			if (!$edit_one->id())
			{ // 如果用户 ID 无效，则返回用户管理首页
				//dump($edit_one->id());
				$newgroup= new Config($this->_encodeUTF8($_POST));
				//print_r($form->values());
				$newgroup->save();

				return $this->_redirect(url('default::rulesetting/index'));
			}else{
				
				
				return $this->_redirectMessage(
						'添加失败！', 
						'该规则名已存在，请重试。',
						url('default::default/framework', array('control' => 'rulesetting','acts' => 'index|ruleaddform')));
			}
			
			//if($form->validate($_POST)){
			//dump($_POST);
			//$newgroup= new Config($this->_encodeUTF8($_POST));
			//print_r($form->values());
			//$newgroup->save();
				
			//return $this->_redirect(url('default::rulesetting/index'));
			//}
		//} catch (Exception $e) {
		}catch(QValidation_FailedException $ex){
			$form->invalidate($ex);
			return $this->_redirect(url('default::rulesetting/index'));
//			return $this->_redirectMessage(
//						'添加失败！', 
//						'该规则名已存在，请重试。',
//						url('default::default/framework', array('control' => 'rulesetting','acts' => 'index|ruleaddform')));
		}

		$this->_view['form']=$form;
		//$show_box['title'] = '测试操作';
        //dump($_SESSION);

		}
        else
        {
            // 未登录则转到登录页面
            return $this->_redirect(url('default/login'));
        }
    }

	/**1**/
	function actionModify()
    {
		
		$show_box['title'] = '编辑规则';
        if ($this->_app->currentUserRoles())
        {
			Operate::everyOperate($_SESSION['acl_task_userdata']['account'],'','1',''.$show_box['title'],'');
			
			
			// 查询指定用户 ID
			$edit_one = Config::find('id = ?', $this->_context->id )->query();
			if ( !$edit_one->id() )
			{ // 如果用户 ID 无效，则返回用户管理首页
				return $this->_redirect(url('default::rulesetting/index'));
			}
			
			// 构造表单对象
			$show_box['form'] = $form = new Form_RuleAdd(url('default::rulesetting/modify', array('id' => $this->_context->id)));
			$show_box['form']->_subject = $show_box['title'];
			if ($this->_context->isPOST() && $show_box['form'] -> validate($_POST))
			{
				//dump($_POST);
				// changeProps() 方法可以批量修改  对象的属性，但不会修改只读属性的值
				//$this->_encodeUTF8($_POST)
				$edit_one->changeProps($this->_encodeUTF8($show_box['form']->values()));
				// 保存并重定向浏览器
				$edit_one->save();
				//return $this->_redirect(url('admin::users/index'));
				return $this->_redirectMessage(
						'修改配置成功', 
						'配置资料已经成功修改！',
						url('default::default/framework', array('control' => 'rulesetting','acts' => 'index|ruleaddform')));
				//return $this->_redirect(url('default::default/framework', array('space' => 'admin','control' => 'users','acts' => 'indexpart')));
				//index.php?action=framework&space=admin&control=users&acts=add
			}
			elseif (!$this->_context->isPOST())
			{
				// 如果不是 POST 提交，则把对象值导入表单
				//$edit_one['password'] = '';
				//dump($edit_one);
				//cncategory
				$edit_one['rulename']=iconv("GB2312","UTF-8",$edit_one['rulename']);
				$edit_one['cncategory']=iconv("GB2312","UTF-8",$edit_one['cncategory']);
				//dump($edit_one['rulename']);
				$show_box['form']->import($edit_one);
			}
			$this->_view['show_box'] = $show_box;
			$this->_viewname = 'form';
		}
    }


	/**1**/
	function actionDel()
    {
        $show_box['title'] = '删除规则';
        if ($this->_app->currentUserRoles())
        {
		Operate::everyOperate($_SESSION['acl_task_userdata']['account'],'','1',''.$show_box['title'],'');
		
		//注意设定用户删除的锁定
        if($this->_context->id)
        {
        	$rid = $this->_context->id;
        	Config::meta()->destroyWhere('id = ?', $rid);
        	if(mysql_affected_rows())
        	{
        		return $this->_redirectMessage(
                '规则删除成功', 
                '指定规则删除成功',
                url('default::default/framework', array('control' => 'rulesetting','acts' => 'index|ruleaddform')));
        	}
        	else
        	{
        		return $this->_redirectMessage(
                '规则删除失败', 
                '指定规则删除失败',
                url('default::default/framework', array('control' => 'rulesetting','acts' => 'index|ruleaddform')));
        	}
        }
        else
        {
        	return $this->_redirectMessage(
                '错误操作', 
                '操作错误，返回首页',
                url('default::default/framework', array('control' => 'rulesetting','acts' => 'index|ruleaddform')));
        }

		}
        else
        {
            // 未登录则转到登录页面
            return $this->_redirect(url('default/login'));
        }
    }
	
    /**1**/
    function actionIndex()
    {
        $show_box['title'] = '规则设置首页';
		$groupno=0;$this->_view['gid'] = '';
        if ($this->_app->currentUserRoles())
        {
			Operate::everyOperate($_SESSION['acl_task_userdata']['account'],'','1','查看'.$show_box['title'],'');

			if(isset($this->_context->id)){
				$this->_view['gid'] = $this->_context->id;
				$groupno=$this->_context->id;
			}
			// 设置页面列表数目
			$pagesize = 10;
			// 获取当前页数，通常为get方式参数
			$page = $this->_context->page;
			$this->_view['pageno']=$page;
			$select = Config::find('groupid = ?',$groupno)->limitPage($page, $pagesize);
			$this->_view['pager'] = $select->getPagination();
			$this->_view['group'] = $select->order("id desc")->getAll();
			$this->_view['countno'] = $this->_view['pager']['page_size']*$page+1;
			if($this->_view['pager']['record_count']>0 && $page=='')
				$page=1;

			$pagediv = new Helper_JsPager($this->_view['pager']['record_count'], $this->_view['pager']['page_size'], 9, $page, 'opgopage(\'rulesetting|index|','\')',5);
			$this->_view['pagestr']= $pagediv->show();
        }
        else
        {
            // 未登录则转到登录页面
            return $this->_redirect(url('default/login'));
        }
    }

	/**1**/
	function actionIndexguolv()
    {
        $show_box['title'] = '过滤规则设置首页';
		$groupno=0;$this->_view['gid'] = '';
        if ($this->_app->currentUserRoles())
        {
			Operate::everyOperate($_SESSION['acl_task_userdata']['account'],'','1','查看'.$show_box['title'],'');
			
			if(isset($this->_context->id)){
				$groupno=$this->_context->id;
				$this->_view['gid'] = $this->_context->id;
			}
			// 设置页面列表数目
			$pagesize = 10;
			// 获取当前页数，通常为get方式参数
			$page = $this->_context->page;
			$this->_view['pageno']=$page;
			$select = Config::find('ruleflag = ? and groupid = ?','1',$groupno)->limitPage($page, $pagesize);
			$this->_view['pager'] = $select->getPagination();
			$this->_view['group'] = $select->order("id desc")->getAll();
			$this->_view['countno'] = $this->_view['pager']['page_size']*$page+1;
			if($this->_view['pager']['record_count']>0 && $page=='')
				$page=1;
			
			$pagediv = new Helper_JsPager($this->_view['pager']['record_count'], $this->_view['pager']['page_size'], 9, $page, 'opgopage(\'rulesetting|indexguolv|','\')',5);
			$this->_view['pagestr']= $pagediv->show();
        }
        else
        {
            // 未登录则转到登录页面
            return $this->_redirect(url('default/login'));
        }
    }

	/**1**/
	function actionRules()
    {
		$show_box['title'] = '组规则设置首页';
        if ($this->_app->currentUserRoles())
        {
			Operate::everyOperate($_SESSION['acl_task_userdata']['account'],'','1','查看'.$show_box['title'],'');
			// 设置页面列表数目
			$pagesize = 10;
			// 获取当前页数，通常为get方式参数
			$page = $this->_context->page;
			$this->_view['pageno']=$page;
			$select = Hostgroup::find()->limitPage($page, $pagesize);
			$this->_view['pager'] = $select->getPagination();
			$this->_view['group'] = $select->order("id desc")->getAll();
			$this->_view['countno'] = $this->_view['pager']['page_size']*$page+1;
			if($this->_view['pager']['record_count']>0 && $page=='')
				$page=1;
			$pagediv = new Helper_JsPager($this->_view['pager']['record_count'], $this->_view['pager']['page_size'], 9, $page, 'opgopage(\'hostmanage|rules|','\')',5);
			$this->_view['pagestr']= $pagediv->show();

			//统计资料
			//SELECT count( 1 ) AS configs, groupid FROM `config` GROUP BY groupid
			$table =QDB::getConn();
			$sql = "SELECT count(id) AS configall, groupid FROM `config` GROUP BY groupid";
			$handel =$table->execute($sql);
			$temp2 = $handel->fetchAll();
			$achosts = array();
			foreach ($temp2 as $s){
				if(isset($s['configall']))
					$achosts[$s['groupid']]=$s['configall'];
				//array_push($achosts,$s['configall']);
			}
			$this->_view['group_config']= $achosts;
			//dump($achosts);
		}
    }	

	/**1**/
	function actionIndexbaojing()
    {
        $show_box['title'] = '报警规则设置首页';
		$groupno=0;$this->_view['gid'] = '';
        if ($this->_app->currentUserRoles())
        {
			Operate::everyOperate($_SESSION['acl_task_userdata']['account'],'','1','查看'.$show_box['title'],'');
			
			if(isset($this->_context->id)){
				$groupno=$this->_context->id;
				$this->_view['gid'] = $this->_context->id;
			}
			// 设置页面列表数目
			$pagesize = 10;
			// 获取当前页数，通常为get方式参数
			$page = $this->_context->page;
			$this->_view['pageno']=$page;
			$select = Config::find('ruleflag = ? and groupid = ?','0',$groupno)->limitPage($page, $pagesize);
			$this->_view['pager'] = $select->getPagination();
			$this->_view['group'] = $select->order("id desc")->getAll();
			$this->_view['countno'] = $this->_view['pager']['page_size']*$page+1;
			if($this->_view['pager']['record_count']>0 && $page=='')
				$page=1;

			$pagediv = new Helper_JsPager($this->_view['pager']['record_count'], $this->_view['pager']['page_size'], 9, $page, 'opgopage(\'rulesetting|indexbaojing|','\')',5);
			$this->_view['pagestr']= $pagediv->show();
        }
        else
        {
            // 未登录则转到登录页面
            return $this->_redirect(url('default/login'));
        }
    }

	/**1**/
	function actionIndexgroup()
    {
        $show_box['title'] = '组规则设置首页';
        if ($this->_app->currentUserRoles())
        {
			Operate::everyOperate($_SESSION['acl_task_userdata']['account'],'','1','查看'.$show_box['title'],'');
			
			$groupno=$this->_context->id;

			if(isset($this->_context->groupid))
				$groupno=$this->_context->groupid;
			// 设置页面列表数目
			$pagesize = 10;
			// 获取当前页数，通常为get方式参数
			$page = $this->_context->page;
			$this->_view['pageno']=$page;
			$select = Config::find('groupid = ?',$groupno)->limitPage($page, $pagesize);
			$this->_view['pager'] = $select->getPagination();
			$this->_view['group'] = $select->order("id desc")->getAll();
			$this->_view['gid'] = $this->_context->id;
			$this->_view['countno'] = $this->_view['pager']['page_size']*$page+1;
			if($this->_view['pager']['record_count']>0 && $page=='')
				$page=1;

			$pagediv = new Helper_JsPager($this->_view['pager']['record_count'], $this->_view['pager']['page_size'], 9, $page, 'opgopage(\'rulesetting|indexbaojing|','\')',5);
			$this->_view['pagestr']= $pagediv->show();
        }
        else
        {
            // 未登录则转到登录页面
            return $this->_redirect(url('default/login'));
        }
    }

	protected function _encodeUTF8($array){



	   foreach($array as $key=>$value){

		//$key=iconv("GB2312","UTF-8",$key);
			if($key!=''){
	      $array[$key] = !is_array($value)?iconv("UTF-8","GB2312",$value):self::_encodeUTF8($array[$key]);
			}
			

	   }
	   $newarray=array();
		foreach($array as $key=>$value){
		if($array[$key]!='')
		$newarray[iconv("UTF-8","GB2312",$key)]=$array[$key];
		
		}
		

	   return $newarray;



	}
}